<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no" />
  <title>04-待办列表.html</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      background-color: #ccc;
    }

    ul {
      list-style: none;
    }

    li {
      padding: 20px;
      text-align: left;
      font-size: 30px;
      border-bottom: 1px dashed #ccc;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    li input {
      margin-right: 10px;
    }

    li button {
      display: none;
      padding: 5px;
    }

    li:hover button {
      display: inline-block;
      cursor: pointer;
    }

    .chk:checked+span {
      text-decoration: line-through;
    }

    h1 {
      margin-bottom: 10px;
    }

    .chk+span {}

    .box {
      background-color: #fff;
      width: 60vw;
      padding: 20px 20px 0;
      margin: 50px auto;
    }

    .box .tool input {
      width: 100%;
      height: 50px;
      text-indent: 20px;
      font-size: 20px;
      font-style: italic;
      color: #666;
      font-weight: 700;
    }

    section {
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    a {
      text-decoration-color: #666;
      color: inherit;
    }
  </style>
</head>

<body>
  <div id="app" data-v-app="">
    <div class="box">
      <h1>待办列表</h1>
      <div class="tool">
        <input id="word" autofocus="" type="text" placeholder="请输入代办事项" />
      </div>
      <ul>

        <!--  <li>
          <div>
            <input type="checkbox" />
            <span class="false">111</span>
          </div>
          <button id="">X</button>
        </li> -->

      </ul>
      <section>
        <span><i>1</i> 未完成</span><a href="#">清理 <b>0</b> 已完成</a>
      </section>
    </div>
  </div>
  <script>



    const arr = JSON.parse(localStorage.getItem('data')) || []
    const ul = document.querySelector('ul')

    function render() {
      let num = 0
      const newArr = arr.map(function (item, index) {
        if (item.isChecked == false) {
          num++
        }
        return `
              <li>
                  <div>
                     <input  data-id=${index} type="checkbox" ${item.isChecked ? 'checked' : ''}  class="chk"} />
                     <span class="false">${item.content}</span>
                  </div>
                 <button id="del" data-id=${index}>X</button>
              </li>
    `
      })

      ul.innerHTML = newArr.join('')

      const sum = document.querySelector('span i')
      sum.innerHTML = num

      const qinli = document.querySelector('b')
      qinli.innerHTML = arr.length - num

    }
    render()


    const input = document.querySelector('#word')

    input.addEventListener('keyup', function (e) {
      ''
      if (e.key == 'Enter' && this.value != '') {
        const content = {
          content: this.value,
          isChecked: false
        }
        this.value = ''
        arr.push(content)
        localStorage.setItem('data', JSON.stringify(arr))
        render()
      }
    })




    ul.addEventListener('click', function (e) {

      if (e.target.className == 'chk') {
        arr[e.target.dataset.id].isChecked = e.target.checked
        localStorage.setItem('data', JSON.stringify(arr))
        render()
      }
      console.log(e.target.className);
      if (e.target.tagName == 'BUTTON') {
        arr.splice(e.target.dataset.id, 1)
        localStorage.setItem('data', JSON.stringify(arr))
        render()
      }
    })


    const del = document.querySelector('section a')
    del.addEventListener('click', function () {
      arr.forEach(function (item, index) {

      })
    })
  </script>
</body>

</html>